package cn.exrick.utils.mybatis;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Page<T> {

    protected List<T> result = Collections.emptyList();

    protected int totalCount = -1;//总记录数

    protected int pageCount;//总页数

    //其他附加信息
    protected Map<String, Object> map = new HashMap<String, Object>();

    private PageParam pageParam;

    public Page(PageParam pageParam) {
        this.pageParam = pageParam;
    }

    public Page(PageParam pageParam, List<T> result) {
        this.pageParam = pageParam;
        this.result = result;
    }

    /**
     * 取得页内的记录列表.
     */
    public List<T> getResult() {
        return result;
    }

    public void setResult(final List<T> result) {
        this.result = result;
    }

    /**
     * 取得总记录数,默认值为-1.
     */
    public int getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(final int totalCount) {
        if (totalCount >= 0) {
            this.totalCount = totalCount;
            this.pageCount = (totalCount + pageParam.getPageSize() - 1) / pageParam.getPageSize();
        }
    }

    /**
     * 是否还有下一页.
     */
    public boolean isHasNext() {
        return (pageParam.pageNo + 1 < getPageCount());
    }

    /**
     * 取得下页的页号,序号从1开始.
     */
    public int getNextPage() {
        if (isHasNext())
            return pageParam.pageNo + 1;
        else
            return pageParam.pageNo;
    }

    /**
     * 是否还有上一页.
     */
    public boolean isHasPre() {
        return (pageParam.pageNo - 1 >= 0);
    }

    /**
     * 取得上页的页号,序号从1开始.
     */
    public int getPrePage() {
        if (isHasPre())
            return pageParam.pageNo - 1;
        else
            return pageParam.pageNo;
    }

    /**********************************************/

    /**
     * 得到总页数
     *
     * @return
     */
    public int getPageCount() {
//        return this.pageCount > PageParam.MAX_PAGENUM ? PageParam.MAX_PAGENUM : pageCount;
        return this.pageCount;
    }

    public void put(String key, Object value) {
        this.map.put(key, value);
    }

    public PageParam getPageParam() {
        return pageParam;
    }
}

